$.validator.setDefaults({
    submitHandler: function () {
        if(!$("#logoUrl").val()){
            $(".avatar-error").show().html("请上传Logo");
            return false;
        }
        if(!$("#qrUrl").val()){
            $(".qrimg-error").show().html("请上传二维码");
            return false;
        }
        if($("span.del").length<1){
            $(".check-screenshot-error").show().html("请上传截图");
            return false;
        }
       $("#appDesc").val(UE.getEditor('container').getContent());
        $.ajax({
            "url": "/saveApp",
            type: 'post',
            data: $.param($("form").serializeArray()),
            success: function(data){
                alert("发布成功！");
                window.location.reload();
            }
        });
    }
});
$().ready(function () {
    <!-- 实例化编辑器 -->
    var ue = UE.getEditor('container');
    //页面回显点击处理
    bindDelClick();
    //验证
    $("#publishForm").validate({
        rules: {
            appName: "required",
            category: "required",
            tagsArr: "required",
            qq: "required",
            appSum: "required",
            //username: {
            //    required: true,
            //    minlength: 2
            //},
            //password: {
            //    required: true,
            //    minlength: 5
            //},
            //confirm_password: {
            //    required: true,
            //    minlength: 5,
            //    equalTo: "#password"
            //},
            //email: {
            //    required: true,
            //    email: true
            //},
            //topic: {
            //    required: "#newsletter:checked",
            //    minlength: 2
            //},
            //agree: "required"
        },
        messages: {
            appName: "请输入小程序名称",
            category: "请选择小程序分类",
            tagsArr: "请输入小程序的标签",
            qq: "请输入您的QQ号",
            appSum: "请输入小程序的用途",
            //username: {
            //    required: "请输入用户名",
            //    minlength: "用户名必需由两个字母组成"
            //},
            //password: {
            //    required: "请输入密码",
            //    minlength: "密码长度不能小于 5 个字母"
            //},
            //confirm_password: {
            //    required: "请输入密码",
            //    minlength: "密码长度不能小于 5 个字母",
            //    equalTo: "两次密码输入不一致"
            //},
            //email: "请输入一个正确的邮箱",
            //agree: "请接受我们的声明",
            //topic: "请选择两个主题"
        }
    })
})
$(".upload").on('change',function(){
    var id=$(this).attr("id");
    $(this).parents("div").find("label.error").hide();
    upPhoto(id);
});

function bindDelClick(){
    $("span.del").click(function(){
        $(this).parent("div.screenshot-item").remove();
        checkImages();
    });
    checkImages();
}
function checkImages(){
    //判断是否大于5个
    if($("span.del").length>=6){
        $("#images").hide();
    }else{
        $("#images").show();
    }
}

function upPhoto(id) {
    var url = ctx + "/uploadFile/upPhoto";//这里填请求的地址
    $.ajaxFileUpload({
        url: url,
        type: 'POST',
        fileElementId: id,  //这里对应html中上传file的id
        contentType: "application/json;charset=UTF-8",
        success: function (data) {
            var str = $(data).find("body").text();//获取返回的字符串
            var json = $.parseJSON(str);//把字符串转化为json对象
            var imgUrl=$("#imgServer").val() + json.fileServerName;
            if(id.indexOf("multi")>=0){
                var html='<div class="file uploaded screenshot-item"><img style="display:block" class="preview" src="'+imgUrl+'" width="120" alt=""><input class="fileurl" type="hidden" name="scrUrlsArr" value="'+json.fileServerName+'"><span class="del">删除</span></div>';
                $(".screenshot-list").append(html);
                bindDelClick();
            }else{
                $('#'+id).siblings("img").show().attr("src",imgUrl);
                $('#'+id).siblings("input.fileurl").val(json.fileServerName);
            }
            //上传相同文件名change事件不触发，所以清空一下
            $('#'+id).val("");
        },
        error: function () {
            alert("请链接网络");
        }
    })
}
